<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="学生" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentId">
              <JSelectMultiStudent v-model="model.studentId" :multiple='multiple'/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="科目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectId">
              <j-multi-select-tag placeholder="请选择科目" v-model="model.subjectId"
                                  dictCode='cou_subject,subject_name,id,subject_iz_open =1 and del_flag =0  order by subject_order'></j-multi-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="考试卷" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationPaperId">
              <JSelectExaminationPaper v-model="model.examinationPaperId" :multiple='multiple'></JSelectExaminationPaper>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="achievementScore">
              <a-input-number v-model="model.achievementScore" placeholder="请输入分数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="考试日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="achievementDate">
              <j-date placeholder="请选择考试日期" v-model="model.achievementDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="修改人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateBy">-->
<!--              <a-input v-model="model.updateBy" placeholder="请输入修改人"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { getDepartDataList, getClassStudentQueryAll} from '@api/commonAPI'
import { getSubjectList, getAllAchExaminationPaperList,getAllAchExaminationPaper} from '@api/achievementCommonAPI'
import JSelectMultiStudent from '../../components/public/JSelectMultiStudent'
import JSelectExaminationPaper from '../../components/public/JSelectExaminationPaper'
export default {
  name: 'AchAchievementSubjectForm',
  components: {
    JSelectMultiStudent,
    JSelectExaminationPaper
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      model:{
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
      },
      url: {
        add: "/achachievementsubject/achAchievementSubject/add",
        edit: "/achachievementsubject/achAchievementSubject/edit",
        queryById: "/achachievementsubject/achAchievementSubject/queryById"
      },
      departDataList: [],
      subjectList: [],
      studentList: [],
      achExaminationPaperList: [],
      multiple: false,
      returnId: true
    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  created () {
  },
  mounted() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    this.getDepartData();
    this.getSubjectData();
    this.getStudentData();
    this.getAchExaminationPaperData();
  },
  methods: {
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.model.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          httpAction(httpurl,this.model,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    async getDepartData() {
      let res = await getDepartDataList()
      this.departDataList = []
      res.result.forEach(item => {
        this.departDataList.push({
          label: item.departName,
          value: item.orgCode
        })
      })
    },
    async getSubjectData() {
      let res = await getSubjectList()
      this.subjectList = []
      res.result.records.forEach(item => {
        this.subjectList.push({
          label: item.subjectName,
          value: item.id
        })
      })
    },
    async getStudentData() {
      let res = await getClassStudentQueryAll()
      this.studentList = []
      res.result.forEach(item => {
        this.studentList.push({
          label: item.studentName,
          value: item.id
        })
      })
    },
    async getAchExaminationPaperData() {
      let res = null;
      if(!this.model.subjectId){
         res = await getAllAchExaminationPaperList()
      }else {
         res = await getAllAchExaminationPaper(this.model.subjectId)
      }

      this.achExaminationPaperList = []
      res.result.records.forEach(item => {
        this.achExaminationPaperList.push({
          label: item.examinationPaperName,
          value: item.id
        })
      })
    },

  }
}
</script>